package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.VisitorRecord;

public class VisitorRecordRowMapper implements RowMapper<VisitorRecord> {

    @SuppressWarnings("unused")
    private int type;

    public VisitorRecordRowMapper() {
    }

    public VisitorRecordRowMapper(int type) {
        this.type = type;
    }

    @Override
    public VisitorRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new VisitorRecord(), rs);
    }

    public static VisitorRecord parseHome(VisitorRecord h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static VisitorRecord parseHome(VisitorRecord h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setVrid(rs.getLong(su+"vrid"));
        } catch (SQLException e) {
            h.setVrid(null);
        }
        try {
            h.setVuid(rs.getLong(su+"vuid"));
        } catch (SQLException e) {
            h.setVuid(null);
        }
        try {
            h.setBuid(rs.getLong(su+"buid"));
        } catch (SQLException e) {
            h.setBuid(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, VisitorRecord h) {
        par.put("vrid",h.getVrid());
        par.put("vuid",h.getVuid());
        par.put("buid",h.getBuid());
        par.put("cdate",h.getCdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, VisitorRecord h) {
        args.put("vrid",h.getVrid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final VisitorRecord h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "Vrid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(VisitorRecord h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into visitor_record(");
        int count = 0;
        if (null != h.getVuid()) {
            sql.append("vuid,");
            count++;
        }
        if (null != h.getBuid()) {
            sql.append("buid,");
            count++;
        }
        
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, VisitorRecord h)
        throws SQLException {
        int count = 1;
        if (null != h.getVuid()) {
            ps.setLong(count++,h.getVuid());
        }
        if (null != h.getBuid()) {
            ps.setLong(count++,h.getBuid());
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
